package com.example.event_resource_java.trainapi.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.event_resource_java.authority.dao.StatisticDao.CoursesDO;
import com.example.event_resource_java.trainapi.dao.JzChapterDo;
import com.example.event_resource_java.trainapi.dao.TrainingResourcesDO;
import com.example.event_resource_java.trainapi.dao.vo.ChapterVo;
import com.example.event_resource_java.trainapi.dao.vo.CoursesVo;
import com.example.event_resource_java.trainapi.dao.vo.TrainingResourcesVo;
import com.example.event_resource_java.trainapi.mapper.CoursesMapper;
import com.example.event_resource_java.trainapi.service.CoursesService;
import com.example.event_resource_java.trainapi.service.JzChapterService;
import com.example.event_resource_java.trainapi.service.TrainingResourcesService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 
 * <pre>
 * 课程表
 * </pre>
 * <small> 2020-02-24 21:00:12 | lance</small>
 */
@Service
public class CoursesServiceImpl extends ServiceImpl<CoursesMapper, CoursesDO> implements CoursesService {

    @Autowired
    private CoursesMapper coursesMapper;
    @Autowired
    private JzChapterService jzChapterService;
    @Autowired
    private TrainingResourcesService trainingResourcesService;


    @Override
    public List<CoursesVo> getCourses(String name, String level) {
        QueryWrapper<CoursesDO> wrapper = new QueryWrapper<CoursesDO>().orderByAsc("id");
        List<CoursesVo> list=new ArrayList<>();
        if (StringUtils.isNotBlank(level)){
            level="（"+level+"）";
            wrapper.like("name",name+level);
        } else if(StringUtils.isNotBlank(name)) {
            wrapper.like("name",name);
        }
        wrapper.ne("new_type",6);
        wrapper.eq("state",0);
        List<CoursesDO> coursesDOS = this.list(wrapper);

        for (CoursesDO trainingResourcesDO:coursesDOS) {
            CoursesVo vo = new CoursesVo();
            BeanUtils.copyProperties(trainingResourcesDO, vo);
            list.add(vo);
        }
        return list;
    }

    @Override
    public CoursesVo getCoursesById(Long id) {
        CoursesVo vo=new CoursesVo();
        CoursesDO coursesDO = this.getById(id);
        BeanUtils.copyProperties(coursesDO,vo);
        List<ChapterVo> voList=new ArrayList<>();
        List<JzChapterDo> list = jzChapterService.list(new QueryWrapper<JzChapterDo>().eq("courses_id", id));
        List<TrainingResourcesVo> vos=new ArrayList<>();
        for (JzChapterDo jzChapterDo:list){
            ChapterVo chapterVo=new ChapterVo();
            BeanUtils.copyProperties(jzChapterDo,chapterVo);
            chapterVo.setChapterId(jzChapterDo.getId());
            chapterVo.setParentId(jzChapterDo.getParentId());
            List<TrainingResourcesDO> list1 = trainingResourcesService.list(new QueryWrapper<TrainingResourcesDO>().eq("chapter_id", jzChapterDo.getId()).eq("courses_id", id));
            list1.forEach(chapterId->{
                TrainingResourcesVo resourcesVo=new TrainingResourcesVo();
                resourcesVo.setId(chapterId.getId());
                resourcesVo.setName(chapterId.getName());
                resourcesVo.setFileRoute(chapterId.getFileRoute());
                resourcesVo.setType(chapterId.getType());
                resourcesVo.setVideoDuration(chapterId.getVideoDuration());
                vos.add(resourcesVo);
            });
            chapterVo.setList(vos);
            voList.add(chapterVo);
        }
        vo.setVoList(voList);
        return vo;
    }
}
